package online.sixfish.common.exception;//package com.knox.aurora.common.exception;


import lombok.extern.slf4j.Slf4j;
import online.sixfish.common.api.ApiResult;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 捕获自定义异常
     */
    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public ApiResult<Map<String, Object>> handle(ApiException e) {
        if (e.getErrorCode() != null) {
            return ApiResult.failed(e.getErrorCode());
        }
        return ApiResult.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ApiResult handle(HttpMessageNotReadableException e){
        log.warn("异常:{}-->{}",e.getMessage(),e);
        return ApiResult.failed("请求异常");
    }



    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ApiResult<Map<String, Object>> handle(Exception e) {
        log.warn("异常：{}",e);
        return ApiResult.failed(e.getMessage());
    }
}
